package com.alander.nexusgo.gateway.config;

import cn.dev33.satoken.context.SaHolder;
import cn.dev33.satoken.reactor.filter.SaReactorFilter;
import cn.dev33.satoken.router.SaRouter;
import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.util.SaResult;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class GatewayAuthConfiguration {
    
    /**
     * 注册Sa-Token全局过滤器
     */
    @Bean
    public SaReactorFilter saReactorFilter() {
        return new SaReactorFilter()
            // 拦截所有路由
            .addInclude("/**")
            
            // 排除公开接口
            .addExclude("/favicon.ico")
            
            // 认证校验规则
            .setAuth(obj -> {
                // 配置需要排除的路径
                SaRouter.match("/**")
                    .notMatch("/api/auth/login", "/api/auth/register","/api/auth/oauth2/**" ,"/public/**")
                    .check(r -> StpUtil.checkLogin());
            })
            // 异常处理
            .setError(e -> SaResult.error(e.getMessage()));
    }
}